//
//  FJSettingViewController.swift
//  FruitJuice
//
//  Created by Rui on 2023/12/16.
//

import UIKit

class FJSettingViewController: FJBaseViewController, SAAnimationProtocol {

    private let backButton          = c_make_ex(UIButton.self)
        .image("FJ_Profile_back").end()
    private let titleLabel          = c_make_ex(UILabel.self)
        .text("Setting").color(.black).font(19.font_bold).end()
    private let itemInfos: [FJProfileModel] = [.terms,.privacy,.delete,.clear,.version]
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 60
        tableView.register(FJSettingTableViewCell.self)
        tableView.isScrollEnabled = false

        return tableView
    }()
    private let logoutButton          = c_make_ex(UIButton.self)
        .bgColor("#FF9133".toColor).corner(25).font(21.font_bold)
        .text("Sign out").color(.white).end()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigation.bar.isHidden = true
        view.subviews{
            backButton
            titleLabel
            tableView
            logoutButton
        }
        
        backButton.top(.statusHeight + 7).leading(15).size(30)
        titleLabel.leading(60)
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).height(300)
        tableView.Top == backButton.Bottom + 22
        logoutButton.leading(37.5).trailing(37.5).bottom(.safe_bottom + 80).height(50)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }

    @objc private func clickBackButton(){
        self.navigationController?.popViewController(animated: true)
    }
}

extension FJSettingViewController: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        itemInfos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(FJSettingTableViewCell.self, indexPath: indexPath)
        cell.itemType = itemInfos[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        switch itemInfos[indexPath.row]{
        case .delete:
            let deleteV = FJDeleteAccountView()
            saAnimate.animation(show: deleteV)
            break
        case .terms:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Terms of use"
//            webVC.webUrl = URLTool.technicalSupportUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
            break
        case .privacy:
//            let webVC = LearningWebViewController()
//            webVC.titleText = "Privacy Policy"
//            webVC.webUrl = URLTool.privacyPolicyUrl
//            self.navigationController?.pushViewController(webVC, animated: true)
            break
        case .clear:
//            let clearView = LearningClearCacheView()
//            clearView.didCleanCompletion = { [weak self] in
//                self?.tableView.reloadData()
//            }
//            saAnimate.animation(show: clearView)
            break
        default:
            return
        }

    }
}

class FJSettingTableViewCell: UITableViewCell {
    
    private let titleLabel          = c_make_ex(UILabel.self)
        .color(.black).font(18.font_bold).end()
    private let rightImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("FJ_Profile_setting_right").end()
    private let rightLabel = c_make_ex(UILabel.self).color("#B5B5B5")
        .font(15.font_bold).alignment(.right).end()
    private let lineView = c_make_ex(UIView.self)
        .bgColor("#E8E8E8").end()
    
    var itemType: FJProfileModel = .none {
        didSet {
            titleLabel.text = itemType.title
            if itemType.isShowRightImageView {
                rightImageView.isHidden = false
                rightLabel.isHidden = true
            }else{
                rightImageView.isHidden = true
                rightLabel.isHidden = false
                rightLabel.text = itemType.content
            }
        }
    }
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            titleLabel
            rightImageView
            rightLabel
            lineView
        }
        titleLabel.leading(20).centerVertically()
        rightLabel.trailing(20).centerVertically()
        rightImageView.trailing(15).width(12).height(22).centerVertically()
        lineView.leading(15).trailing(15).bottom(0).height(1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
